package com.zftlive.android.library.tools.sharesdk;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.zftlive.android.library.R;
import com.zftlive.android.library.base.BaseMAdapter;

import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.system.text.ShortMessage;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 自定义分享面板
 *
 * @author 曾繁添
 * @version  1.0
 */
public class ShareItemPannel extends Dialog {

    static final String PLATFORM = "platform";
    static final String LABEL = "label";
    static final String ICON = "icon";
    ShareGridAdapter mGridAdapter;
    Activity mActivity;
    /**
     * 分享数据
     */
    Platform.ShareParams mShareParams;

    public ShareItemPannel(Activity context) {
        super(context, R.style.DialogTopButtomAnimation);
        this.mActivity = context;
        initShareData("", "", "","");
        init();
    }

    public ShareItemPannel(Activity context,String imageURL,String title,String strContent,String linkURL) {
        super(context, R.style.DialogTopButtomAnimation);
        this.mActivity = context;
        initShareData(imageURL, title, strContent,linkURL);
        init();
    }

    /**
     * 初始化分享数据
     * @param imageURL
     * @param title
     * @param strContent
     */
    public void initShareData(String imageURL,String title,String strContent,String linkURL){
        mShareParams = new Platform.ShareParams();
        mShareParams.setShareType(Platform.SHARE_WEBPAGE);
        mShareParams.setTitle(title);
        mShareParams.setImageUrl(imageURL);
        mShareParams.setText(strContent);
        mShareParams.setTitleUrl(linkURL);
        //QQ空间需要SiteUrl
        mShareParams.setSiteUrl(linkURL);
        //微信分享网页、视频需要
        mShareParams.setUrl(linkURL);
    }

    /**
     * 初始化
     */
    private void init(){
        setContentView(R.layout.common_sharesdk_grid_dialog);

        // 设置窗体显示的位置和宽度
        getWindow().setGravity(Gravity.BOTTOM);
        // 是否撑满屏幕宽度
        WindowManager.LayoutParams windowparams = getWindow().getAttributes();
        windowparams.width = mActivity.getResources().getDisplayMetrics().widthPixels;
        getWindow().setAttributes(windowparams);
        // 点击其他区域是否关闭窗体
        setCanceledOnTouchOutside(false);

        //取消按钮
        findViewById(R.id.share_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });

        //分享面板
        GridView shareGrid = (GridView)findViewById(R.id.share_gridView);
        mGridAdapter = new ShareGridAdapter(mActivity);
        shareGrid.setAdapter(mGridAdapter);

        //装填分享数据源
        Platform[] platforms = ShareSDK.getPlatformList();
        if(null == platforms){
            return;
        }
        for (int i = 0;i < platforms.length;i++ ){
            Platform mPlatform = platforms[i];
            String platform = mPlatform.getName();
            Map<String,Object> item = new HashMap<String,Object>();
            item.put(PLATFORM,platform);
            if(SinaWeibo.NAME.equals(platform)){
                item.put(ICON,R.drawable.ssdk_oks_classic_sinaweibo);
                item.put(LABEL,mActivity.getResources().getString(R.string.ssdk_sinaweibo));
            }else if(Wechat.NAME.equals(platform)){
                item.put(ICON,R.drawable.ssdk_oks_classic_wechat);
                item.put(LABEL,mActivity.getResources().getString(R.string.ssdk_wechat));
            }else if(WechatMoments.NAME.equals(platform)){
                item.put(ICON,R.drawable.ssdk_oks_classic_wechatmoments);
                item.put(LABEL,mActivity.getResources().getString(R.string.ssdk_wechatmoments));
            }else if(ShortMessage.NAME.equals(platform)){
                item.put(ICON,R.drawable.ssdk_oks_classic_shortmessage);
                item.put(LABEL,mActivity.getResources().getString(R.string.ssdk_shortmessage));
            }else if(QQ.NAME.equals(platform)){
                item.put(ICON,R.drawable.ssdk_oks_classic_qq);
                item.put(LABEL,mActivity.getResources().getString(R.string.ssdk_qq));
            }else if(QZone.NAME.equals(platform)){
                item.put(ICON,R.drawable.ssdk_oks_classic_qzone);
                item.put(LABEL,mActivity.getResources().getString(R.string.ssdk_qzone));
            }
            mGridAdapter.addItem(item);
        }
        mGridAdapter.notifyDataSetChanged();

        //设置点击监听事件
        shareGrid.setOnItemClickListener(new GridOnItemClickListener());
    }

    /**
     * 分享网格点击监听器
     */
    class GridOnItemClickListener implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            final Map<String,Object> rowData = (Map<String,Object>) adapterView.getItemAtPosition(position);
            if(null == rowData)return;
            String platform = String.valueOf(rowData.get(PLATFORM));
//            ShareSDKHelper.initSDK(mActivity, "843e24ed9a70");
//            ShareSDKHelper.initSDK(mActivity);
            if(SinaWeibo.NAME.equals(platform)){
                ShareSDKHelper.shareSinaWeibo(mShareParams);
            }else if(Wechat.NAME.equals(platform)){
                ShareSDKHelper.shareWeChatFriend(mShareParams);
            }else if(WechatMoments.NAME.equals(platform)){
                ShareSDKHelper.shareWechatMoments(mShareParams);
            }else if(ShortMessage.NAME.equals(platform)){
                ShareSDKHelper.shareShortMessage(mShareParams);
            }else if(QQ.NAME.equals(platform)){
                ShareSDKHelper.shareQQ(mShareParams);
            }else if(QZone.NAME.equals(platform)){
                ShareSDKHelper.shareQQZone(mShareParams);
            }
            //关闭对话框
            ShareItemPannel.this.dismiss();
        }
    }

    /**
     * 网格列表适配器
     */
    class ShareGridAdapter extends BaseMAdapter {

        public ShareGridAdapter(Activity context){
            super(context);
        }

        @Override
        public View getView(int position, View itemView, ViewGroup parent) {
            // 查找控件
            ViewHolder holder = null;
            if (null == itemView) {
                itemView =
                        LayoutInflater.from(getActivity()).inflate(R.layout.common_sharesdk_grid_item, parent,
                                false);
                holder = new ViewHolder();
                holder.tv_label = (TextView) itemView.findViewById(R.id.tv_label);
                holder.iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
                // 缓存View
                itemView.setTag(holder);
            } else {
                holder = (ViewHolder) itemView.getTag();
            }

            // 装填数据
            final Map<String,Object> rowData = (Map<String,Object>) getItem(position);
            holder.tv_label.setText(String.valueOf(rowData.get(LABEL)));
            // 图标
            int resId = (Integer) rowData.get(ICON);
            holder.iv_icon.setImageResource(resId);
            return itemView;
        }
    }

    class ViewHolder {
        TextView tv_label;
        ImageView iv_icon;
    }

}
